package xs.szw.service.controller.auth;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.security.PermitAll;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import xs.szw.common.result.Result;
import xs.szw.common.result.ResultCode;
import xs.szw.common.utils.ServletUtils;
import xs.szw.service.model.dto.user.UserDto;
import xs.szw.service.model.pojo.captcha.CaptchaInfo;
import xs.szw.service.model.pojo.security.AuthenticationToken;
import xs.szw.service.service.auth.AuthService;

import static xs.szw.common.result.Result.success;


/**
 * @program: xs_oa
 * @description:
 * @author: Songzw
 * @create: 2025-02-06 16:27
 **/
@Tag(name = "认证中心")
@RestController
@RequestMapping("/sys")
@Slf4j
@RequiredArgsConstructor
public class AuthController {


    private final AuthService authService;


    @GetMapping("/captcha")
    @PermitAll
    @Operation(summary = "获得验证码")
    public Result<CaptchaInfo> getCaptcha(){
        CaptchaInfo captcha = authService.getCaptcha();
        return success(captcha,"获得验证码");
    }

    @PostMapping("/login")
    @PermitAll
    @Operation(summary ="登录系统")
    public Result<Object> login(@RequestBody UserDto userDto,HttpServletRequest request){
        log.info("remote IP :{}", ServletUtils.getIpAddress(request));
        AuthenticationToken token = authService.login(userDto);
        return success(token, ResultCode.LOGIN_SUCCESS.getMessage());
    }



    @PostMapping("/logout")
    @PermitAll
    @Operation(summary = "登出系统")
    @Parameters({
            @Parameter(name = "token",required = true,description = "jwt令牌",in = ParameterIn.HEADER)
    })
    public Result<Object> logOut(){
        authService.logOut();
        return success(null, ResultCode.LOGIN_FAIL.getMessage());
    }
}
